package exercises.leetcode;

import java.util.LinkedList;
import java.util.NoSuchElementException;

/**
 * <a href="https://leetcode-cn.com/problems/implement-queue-using-stacks/">
 * 232. 用栈实现队列</a>
 *
 * <p><span color="#87CEEB">描述:</span>
 * 经典
 *
 * @author or2
 * @date 2021年10月02日 时间: 19:53
 */
public class No232 {

    private LinkedList<Integer> stack = new LinkedList<>();
    private LinkedList<Integer> auxiliary = new LinkedList<>();

    public No232() {
    }

    public void push(int x) {
        stack.push(x);
    }

    public int pop() {
        if (auxiliary.isEmpty())
            while (!stack.isEmpty())
                auxiliary.push(stack.pop());
        if (auxiliary.isEmpty())
            throw new NoSuchElementException();
        else
            return auxiliary.pop();
    }

    public int peek() {
        if (auxiliary.isEmpty())
            while (!stack.isEmpty())
                auxiliary.push(stack.pop());
        if (auxiliary.isEmpty())
            throw new NoSuchElementException();
        else
            return auxiliary.peek();
    }

    public boolean empty() {
        return auxiliary.isEmpty() && stack.isEmpty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
